package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author zyy
 * @date 2020/12/29 上午8:12
 * @description 把请求信息封装成Request对象(根据InputStream进行封装)
 */
public class Request {
    private String method; //请求方式 比如GET,POST
    private String url; //例如 /,/index

    private InputStream inputStream; //输入流 其他属性从InputStream解析出来

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        //从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr =  new String(bytes);
        String firstLine = inputStr.split("\\n")[0]; //GET / HTTP/1.1
        String[] strings = firstLine.split(" ");
        this.method = strings[0];
        // 将主机名和应用名称以及资源名称进行拼接
        String[] host = inputStr.split("\\r\\n")[1].split(":");
        this.url = (host[1]+strings[1]).trim();

        System.out.println("method = " + method);
        System.out.println("url = " + url);

    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
